package com.wll.design.策略.service.impl;

import com.wll.design.策略.service.IPay;
import com.wll.design.core.utils.MyStringutils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

/**
 * @Description: 动态拼接名称
 * @Author: Wanglulu
 * @Date: 2021/1/4 10:46
 * @Version 0.0.1
 */
@Service
public class PayServiceImplAware implements ApplicationContextAware {

    public ApplicationContext applicationContext;

    public static final String SUFFIX = "Pay";


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void toPay(String payCode) {
        IPay iPay = (IPay) applicationContext.getBean(getBeanName(payCode));
        if (iPay != null) {
            iPay.pay();
        } else {
            System.out.println("没有用该支付方式：" + payCode);
        }
    }

    public String getBeanName(String payCode) {

        return MyStringutils.toLowerCaseFirstOne(payCode) + SUFFIX;
    }
}
